package com.kkd.kkdapp.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import pl.droidsonroids.gif.GifDrawable;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.SignList;
import com.kkd.kkdapp.http.entity.SingInData;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.AutoScrollTextView;
import com.kkd.kkdapp.view.AutoScrollTextView.Mode;
import com.kkd.kkdapp.view.SlipButton;
import com.kkd.kkdapp.view.SlipButton.OnChangedListener;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

/*****
 * 
 * @Name SignActivity(签到)
 * @Date 2016-07-15 11:32
 * @author mty
 * 
 */
public class SignActivity extends BaseActivity implements OnClickListener {

	private final int SIGN_CODE_ZREO = 1;
	private final int SIGN_CODE_ELSE = 2;
	private final int SIGN_SWITCH_ZREO = 3;
	private final int SIGN_SWITCH_ELSE = 4;
	private final int RETROACTIVE_DATA_OK = 5;
	private final int RETROACTIVE_DATA_NO = 6;
	private final int REPAIRSIGNIN_OK = 7;
	private final int REPAIRSIGNIN_NO = 8;
	private RelativeLayout title_img; // 返回
	private RelativeLayout regulations; // 签到规则
	private int signDays = 0;
	private RelativeLayout exchange_gifts; // 兑换礼品
	private RelativeLayout shake_qian_shu; // 摇钱树
	private SlipButton switchbutton; // 签到提醒开关
	private LinearLayout retroactive_bt; // 补签按钮
	private LinearLayout complete_sign_bt; // 完成任务
	private Dialog retroactiveDialog; // 补签弹窗
	private SingInData data;
	private TextView total_tv; // 快乐币总数
	private TextView gethappycoin_tv; // 获得快乐币个数
	private RelativeLayout signindesc_tv; // 断签描述
	private String coinTaskUrl; // 快乐币任务URL
	private String convertUrl; // 兑换奖品url
	private String goHappyTree; // 摇钱树url
	private ImageView noopen_gift_img1, noopen_gift_img2, noopen_gift_img3,
			noopen_gift_img4; // 礼品盒未打开
	private LinearLayout open_gift_img1, open_gift_img2, open_gift_img3,
			open_gift_img4; // 礼品盒打开
	private TextView open_gift_name1, open_gift_name2, open_gift_name3,
			open_gift_name4; // 优惠券名称
	private TextView open_gift_value1, open_gift_value2, open_gift_value3,
			open_gift_value4; // 优惠券金额
	private TextView sign_desc; // 断签提示语
	private String desc;
	private boolean signSwitch = true;
	private AutoScrollTextView signdays_tv1, signdays_tv2, signdays_tv3; // 签到天数
	private String[] signDays_arr; // 签到天数个位、十位、百位数数组
	private ObjectAnimator signdays1RotationX; // 个位数动画
	private ImageView gift_he_img;
	private ObjectAnimator animator;
	private int startNumber = 0;
	private ImageView sign_img;
	private TextView sign_switch;
	private TextView giftday_tv1, giftday_tv2, giftday_tv3, giftday_tv4;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SIGN_CODE_ZREO: // 签到页接口请求成功
				signDays = data.getSignDays();
				// signDaysRotationAnimator();
				String sSignDays = SystemUtils
						.continuousAttendanceDays(signDays);
				signDays_arr = sSignDays.split("&");
				signdays_tv1.setNumber(startNumber);
				signdays_tv1.setTargetNumber(Integer.parseInt(signDays_arr[0]));
				signdays_tv2.setNumber(startNumber);
				signdays_tv2.setTargetNumber(Integer.parseInt(signDays_arr[1]));
				signdays_tv3.setNumber(startNumber);
				signdays_tv3.setTargetNumber(Integer.parseInt(signDays_arr[2]));

				signPregressLogic();
				int noticeStatus = data.getNoticeStatus(); // 签到提醒状态 0关闭 1开启
				LogUtil.e("TAG", "AAF" + noticeStatus);
				if (noticeStatus == 0) {
					switchbutton.setCheck(false);
					sign_switch.setText("已关闭提醒");
				} else if (noticeStatus == 1) {
					switchbutton.setCheck(true);
					sign_switch.setText("已开启提醒");
				}
				total_tv.setText(data.getAllCoinCount() + "个快乐币");
				gethappycoin_tv.setText("" + data.getGetHappyCoin());
				if (TextUtils.isEmpty(data.getSignInDesc())) {
					signindesc_tv.setVisibility(View.GONE);
				} else {
					signindesc_tv.setVisibility(View.VISIBLE);
					sign_desc.setText(data.getSignInDesc());
					ImageLoader.getInstance().displayImage(
							data.getSignInImage(), sign_img);
				}
				coinTaskUrl = data.getCoinTaskUrl();
				if (TextUtils.isEmpty(data.getCoinTaskUrl())) {
					complete_sign_bt.setVisibility(View.GONE);
				} else {
					complete_sign_bt.setVisibility(View.VISIBLE);
				}
				convertUrl = data.getConvertUrl();
				goHappyTree = data.getGoHappyTree();
				List<SignList> giftArray = data.getGiftArray();

				// 第一个礼品逻辑处理
				if (giftArray.get(0).getStatus() == 0) { // (1已获得，0未获得)
					noopen_gift_img1.setVisibility(View.VISIBLE);
					open_gift_img1.setVisibility(View.INVISIBLE);
					open_gift_name1.setText(giftArray.get(0).getName());
					open_gift_value1.setText("" + giftArray.get(0).getValue());
				} else if (giftArray.get(0).getStatus() == 1) {
					noopen_gift_img1.setVisibility(View.INVISIBLE);
					open_gift_img1.setVisibility(View.VISIBLE);
					open_gift_name1.setText(giftArray.get(0).getName());
					open_gift_value1.setText("" + giftArray.get(0).getValue());
				}
				// 第二个礼品逻辑处理
				if (giftArray.get(1).getStatus() == 0) { // (1已获得，0未获得)
					noopen_gift_img2.setVisibility(View.VISIBLE);
					open_gift_img2.setVisibility(View.INVISIBLE);
					open_gift_name2.setText(giftArray.get(1).getName());
					open_gift_value2.setText("" + giftArray.get(1).getValue());
				} else if (giftArray.get(1).getStatus() == 1) {
					noopen_gift_img2.setVisibility(View.INVISIBLE);
					open_gift_img2.setVisibility(View.VISIBLE);
					open_gift_name2.setText(giftArray.get(1).getName());
					open_gift_value2.setText("" + giftArray.get(1).getValue());
				}
				// 第三个礼品逻辑处理
				if (giftArray.get(2).getStatus() == 0) { // (1已获得，0未获得)
					noopen_gift_img3.setVisibility(View.VISIBLE);
					open_gift_img3.setVisibility(View.INVISIBLE);
					open_gift_name3.setText(giftArray.get(2).getName());
					open_gift_value3.setText("" + giftArray.get(2).getValue());
				} else if (giftArray.get(2).getStatus() == 1) {
					noopen_gift_img3.setVisibility(View.INVISIBLE);
					open_gift_img3.setVisibility(View.VISIBLE);
					open_gift_name3.setText(giftArray.get(2).getName());
					open_gift_value3.setText("" + giftArray.get(2).getValue());
				}
				// 第四个礼品逻辑处理
				if (giftArray.get(3).getStatus() == 0) { // (1已获得，0未获得)
					noopen_gift_img4.setVisibility(View.VISIBLE);
					open_gift_img4.setVisibility(View.INVISIBLE);
					open_gift_name4.setText(giftArray.get(3).getName());
					open_gift_value4.setText("" + giftArray.get(3).getValue());
				} else if (giftArray.get(3).getStatus() == 1) {
					noopen_gift_img4.setVisibility(View.INVISIBLE);
					open_gift_img4.setVisibility(View.VISIBLE);
					open_gift_name4.setText(giftArray.get(3).getName());
					open_gift_value4.setText("" + giftArray.get(3).getValue());
				}
				// 礼品盒对应的天数
				giftday_tv1.setText(giftArray.get(0).getDays());
				giftday_tv2.setText(giftArray.get(1).getDays());
				giftday_tv3.setText(giftArray.get(2).getDays());
				giftday_tv4.setText(giftArray.get(3).getDays());
				// if (giftArray.size() == 0) {
				// noopen_gift_img1.setVisibility(View.VISIBLE);
				// noopen_gift_img2.setVisibility(View.VISIBLE);
				// noopen_gift_img3.setVisibility(View.VISIBLE);
				// noopen_gift_img4.setVisibility(View.VISIBLE);
				// open_gift_img1.setVisibility(View.INVISIBLE);
				// open_gift_img2.setVisibility(View.INVISIBLE);
				// open_gift_img3.setVisibility(View.INVISIBLE);
				// open_gift_img4.setVisibility(View.INVISIBLE);
				// } else if (giftArray.size() == 1) {
				// noopen_gift_img1.setVisibility(View.INVISIBLE);
				// noopen_gift_img2.setVisibility(View.VISIBLE);
				// noopen_gift_img3.setVisibility(View.VISIBLE);
				// noopen_gift_img4.setVisibility(View.VISIBLE);
				// open_gift_img1.setVisibility(View.VISIBLE);
				// open_gift_img2.setVisibility(View.INVISIBLE);
				// open_gift_img3.setVisibility(View.INVISIBLE);
				// open_gift_img4.setVisibility(View.INVISIBLE);
				// open_gift_name1.setText(giftArray.get(0).getName());
				// open_gift_value1.setText("" + giftArray.get(0).getValue());
				// } else if (giftArray.size() == 2) {
				// noopen_gift_img1.setVisibility(View.INVISIBLE);
				// noopen_gift_img2.setVisibility(View.INVISIBLE);
				// noopen_gift_img3.setVisibility(View.VISIBLE);
				// noopen_gift_img4.setVisibility(View.VISIBLE);
				// open_gift_img1.setVisibility(View.VISIBLE);
				// open_gift_img2.setVisibility(View.VISIBLE);
				// open_gift_img3.setVisibility(View.INVISIBLE);
				// open_gift_img4.setVisibility(View.INVISIBLE);
				// open_gift_name1.setText(giftArray.get(0).getName());
				// open_gift_value1.setText("" + giftArray.get(0).getValue());
				// open_gift_name2.setText(giftArray.get(1).getName());
				// open_gift_value2.setText("" + giftArray.get(1).getValue());
				// } else if (giftArray.size() == 3) {
				// noopen_gift_img1.setVisibility(View.INVISIBLE);
				// noopen_gift_img2.setVisibility(View.INVISIBLE);
				// noopen_gift_img3.setVisibility(View.INVISIBLE);
				// noopen_gift_img4.setVisibility(View.VISIBLE);
				// open_gift_img1.setVisibility(View.VISIBLE);
				// open_gift_img2.setVisibility(View.VISIBLE);
				// open_gift_img3.setVisibility(View.VISIBLE);
				// open_gift_img4.setVisibility(View.INVISIBLE);
				// open_gift_name1.setText(giftArray.get(0).getName());
				// open_gift_value1.setText("" + giftArray.get(0).getValue());
				// open_gift_name2.setText(giftArray.get(1).getName());
				// open_gift_value2.setText("" + giftArray.get(1).getValue());
				// open_gift_name3.setText(giftArray.get(2).getName());
				// open_gift_value3.setText("" + giftArray.get(2).getValue());
				// } else if (giftArray.size() == 4) {
				// noopen_gift_img1.setVisibility(View.INVISIBLE);
				// noopen_gift_img2.setVisibility(View.INVISIBLE);
				// noopen_gift_img3.setVisibility(View.INVISIBLE);
				// noopen_gift_img4.setVisibility(View.INVISIBLE);
				// open_gift_img1.setVisibility(View.VISIBLE);
				// open_gift_img2.setVisibility(View.VISIBLE);
				// open_gift_img3.setVisibility(View.VISIBLE);
				// open_gift_img4.setVisibility(View.VISIBLE);
				// open_gift_name1.setText(giftArray.get(0).getName());
				// open_gift_value1.setText("" + giftArray.get(0).getValue());
				// open_gift_name2.setText(giftArray.get(1).getName());
				// open_gift_value2.setText("" + giftArray.get(1).getValue());
				// open_gift_name3.setText(giftArray.get(2).getName());
				// open_gift_value3.setText("" + giftArray.get(2).getValue());
				// open_gift_name4.setText(giftArray.get(3).getName());
				// open_gift_value4.setText("" + giftArray.get(3).getValue());
				// }
				// 是否可以补签，(0不需要，1需要)
				int reSignStatus = data.getReSignStatus();
				if (reSignStatus == 0) {
					retroactive_bt.setVisibility(View.GONE);
				} else if (reSignStatus == 1) {
					retroactive_bt.setVisibility(View.VISIBLE);
				}
				break;
			case SIGN_CODE_ELSE:
				showToast(desc);
				break;
			case SIGN_SWITCH_ZREO:
				if (signSwitch) {
					switchbutton.setCheck(true);
					sign_switch.setText("已开启提醒");
				} else {
					switchbutton.setCheck(false);
					sign_switch.setText("已关闭提醒");
				}

				break;
			case SIGN_SWITCH_ELSE:
				showToast(desc);
				if (signSwitch) {
					switchbutton.setCheck(true);
					sign_switch.setText("已开启提醒");
				} else {
					switchbutton.setCheck(false);
					sign_switch.setText("已关闭提醒");
				}
				break;
			case RETROACTIVE_DATA_OK:
				SingInData data = (SingInData) msg.obj;
				int coinCount = data.getCoinCount();
				List<String> dataList = data.getDataList();
				String reSignExt = data.getReSignExt();
				retroactiveDialog(coinCount, dataList, reSignExt);
				break;
			case RETROACTIVE_DATA_NO:
				String mDesc = (String) msg.obj;
				showToast(mDesc);
				break;
			case REPAIRSIGNIN_OK:
				String sJson = (String) msg.obj;
				signInPageInterface();
				try {
					JSONObject rData = new JSONObject(sJson);
					String signDays = rData.getString("signDays");
					String signCoupon = rData.getString("signCoupon");
					if (TextUtils.isEmpty(signCoupon)) {
						showToast("补签成功\n" + signDays);
					} else {
						showToast("补签成功\n" + signDays + "\n" + signCoupon);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				break;
			case REPAIRSIGNIN_NO:
				String rDesc = (String) msg.obj;
				showToast(rDesc);
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign);
		initView();
		setListener();
		signInPageInterface();
		signPregressLogic();
	}

	/******
	 * 初始化控件
	 ******/
	public void initView() {
		title_img = (RelativeLayout) this.findViewById(R.id.title_img);
		regulations = (RelativeLayout) this.findViewById(R.id.regulations);
		exchange_gifts = (RelativeLayout) this
				.findViewById(R.id.exchange_gifts);
		shake_qian_shu = (RelativeLayout) this
				.findViewById(R.id.shake_qian_shu);
		switchbutton = (SlipButton) this.findViewById(R.id.switchbutton);
		switchbutton.setCheck(true);
		retroactive_bt = (LinearLayout) this.findViewById(R.id.retroactive_bt);
		complete_sign_bt = (LinearLayout) this
				.findViewById(R.id.complete_sign_bt);
		total_tv = (TextView) this.findViewById(R.id.total_tv);
		gethappycoin_tv = (TextView) this.findViewById(R.id.gethappycoin_tv);
		signindesc_tv = (RelativeLayout) this.findViewById(R.id.signindesc_tv);
		noopen_gift_img1 = (ImageView) this.findViewById(R.id.noopen_gift_img1);
		noopen_gift_img2 = (ImageView) this.findViewById(R.id.noopen_gift_img2);
		noopen_gift_img3 = (ImageView) this.findViewById(R.id.noopen_gift_img3);
		noopen_gift_img4 = (ImageView) this.findViewById(R.id.noopen_gift_img4);
		open_gift_img1 = (LinearLayout) this.findViewById(R.id.open_gift_img1);
		open_gift_img2 = (LinearLayout) this.findViewById(R.id.open_gift_img2);
		open_gift_img3 = (LinearLayout) this.findViewById(R.id.open_gift_img3);
		open_gift_img4 = (LinearLayout) this.findViewById(R.id.open_gift_img4);
		open_gift_name1 = (TextView) this.findViewById(R.id.open_gift_name1);
		open_gift_name2 = (TextView) this.findViewById(R.id.open_gift_name2);
		open_gift_name3 = (TextView) this.findViewById(R.id.open_gift_name3);
		open_gift_name4 = (TextView) this.findViewById(R.id.open_gift_name4);
		open_gift_value1 = (TextView) this.findViewById(R.id.open_gift_value1);
		open_gift_value2 = (TextView) this.findViewById(R.id.open_gift_value2);
		open_gift_value3 = (TextView) this.findViewById(R.id.open_gift_value3);
		open_gift_value4 = (TextView) this.findViewById(R.id.open_gift_value4);
		sign_desc = (TextView) this.findViewById(R.id.sign_desc);
		signdays_tv1 = (AutoScrollTextView) this
				.findViewById(R.id.signdays_tv1);
		signdays_tv2 = (AutoScrollTextView) this
				.findViewById(R.id.signdays_tv2);
		signdays_tv3 = (AutoScrollTextView) this
				.findViewById(R.id.signdays_tv3);
		signdays_tv1.setTextSize(18);
		signdays_tv2.setTextSize(18);
		signdays_tv3.setTextSize(18);
		signdays_tv1.setTextColor(Color.parseColor("#FFFFFF"));
		signdays_tv2.setTextColor(Color.parseColor("#FFFFFF"));
		signdays_tv3.setTextColor(Color.parseColor("#FFFFFF"));
		signdays_tv1.setMode(Mode.DOWN_AND_UP);
		signdays_tv2.setMode(Mode.DOWN_AND_UP);
		signdays_tv3.setMode(Mode.DOWN_AND_UP);
		gift_he_img = (ImageView) this.findViewById(R.id.gift_he_img);
		scalAnimation(gift_he_img);
		sign_img = (ImageView) this.findViewById(R.id.sign_img);
		sign_switch = (TextView) this.findViewById(R.id.sign_switch);
		giftday_tv1 = (TextView) this.findViewById(R.id.giftday_tv1);
		giftday_tv2 = (TextView) this.findViewById(R.id.giftday_tv2);
		giftday_tv3 = (TextView) this.findViewById(R.id.giftday_tv3);
		giftday_tv4 = (TextView) this.findViewById(R.id.giftday_tv4);
		GifDrawable drawable;
		try {
			drawable = new GifDrawable(this.getResources(),
					R.drawable.retroactive_img);
			((ImageView) findViewById(R.id.sign_gf)).setImageDrawable(drawable);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/******
	 * 注册监听事件
	 ******/
	public void setListener() {
		title_img.setOnClickListener(this);
		regulations.setOnClickListener(this);
		exchange_gifts.setOnClickListener(this);
		shake_qian_shu.setOnClickListener(this);
		retroactive_bt.setOnClickListener(this);
		complete_sign_bt.setOnClickListener(this);
		switchbutton.SetOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChanged(boolean CheckState) {
				if (CheckState) {
					SystemUtils.onEvent("Account_Sett_CheckIN_ON", 1);
					signSwitch = CheckState;
					updateRemindSatus(1);
				} else {
					SystemUtils.onEvent("Account_Sett_CheckIN_OFF", 1);
					signSwitch = CheckState;
					updateRemindSatus(0);
				}
			}
		});
	}

	Intent intent;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_img: // 返回
			finish();
			break;
		case R.id.regulations: // 签到规则
			intent = new Intent(this, SignRuleActivity.class);
			startActivity(intent);
			break;
		case R.id.exchange_gifts: // 兑换礼品
			if (!TextUtils.isEmpty(convertUrl)) {
				intent = new Intent(this, KKDWebActivity.class);
				intent.putExtra("url", convertUrl);
				startActivity(intent);
			}
			break;
		case R.id.shake_qian_shu: // 摇钱树
			if (!TextUtils.isEmpty(goHappyTree)) {
				intent = new Intent(this, KKDWebActivity.class);
				intent.putExtra("url", goHappyTree);
				startActivity(intent);
			}
			break;
		case R.id.retroactive_bt: // 补签
			beforeRepairSignIn();
			break;
		case R.id.complete_sign_bt: // 快乐币任务
			if (!TextUtils.isEmpty(coinTaskUrl)) {
				intent = new Intent(this, KKDWebActivity.class);
				intent.putExtra("url", coinTaskUrl);
				startActivity(intent);
			}
			break;
		}

	}

	/*****
	 * 签到进度控制逻辑
	 *****/
	public void signPregressLogic() {
		List<Integer> dayList = SystemUtils.signDay(signDays);
		// 当前周期天数
		TextView one_day_tv = (TextView) this.findViewById(R.id.one_day_tv);
		TextView another_day_tv = (TextView) this
				.findViewById(R.id.another_day_tv);
		TextView three_day_tv = (TextView) this.findViewById(R.id.three_day_tv);
		TextView four_day_tv = (TextView) this.findViewById(R.id.four_day_tv);
		TextView five_day_tv = (TextView) this.findViewById(R.id.five_day_tv);
		TextView six_day_tv = (TextView) this.findViewById(R.id.six_day_tv);
		TextView seven_day_tv = (TextView) this.findViewById(R.id.seven_day_tv);
		List<TextView> daytvList = new ArrayList<TextView>();
		daytvList.add(one_day_tv);
		daytvList.add(another_day_tv);
		daytvList.add(three_day_tv);
		daytvList.add(four_day_tv);
		daytvList.add(five_day_tv);
		daytvList.add(six_day_tv);
		daytvList.add(seven_day_tv);
		if (signDays != 0) {
			for (int i = 0; i < dayList.size(); i++) {
				daytvList.get(i).setText(dayList.get(i) + "天");
			}
		} else {
			for (int i = 0; i < dayList.size(); i++) {
				daytvList.get(i).setText((i + 1) + "天");
			}
		}
		// 当前位置图标
		ImageView position_img1 = (ImageView) this
				.findViewById(R.id.position_img1);
		ImageView position_img2 = (ImageView) this
				.findViewById(R.id.position_img2);
		ImageView position_img3 = (ImageView) this
				.findViewById(R.id.position_img3);
		ImageView position_img4 = (ImageView) this
				.findViewById(R.id.position_img4);
		ImageView position_img5 = (ImageView) this
				.findViewById(R.id.position_img5);
		ImageView position_img6 = (ImageView) this
				.findViewById(R.id.position_img6);
		ImageView position_img7 = (ImageView) this
				.findViewById(R.id.position_img7);
		List<ImageView> positionList = new ArrayList<ImageView>();
		positionList.add(position_img1);
		positionList.add(position_img2);
		positionList.add(position_img3);
		positionList.add(position_img4);
		positionList.add(position_img5);
		positionList.add(position_img6);
		positionList.add(position_img7);
		LogUtil.e("TAG", "TB" + ((signDays % 7) - 1));
		if ((signDays % 7) == 0) {
			if (signDays != 0) {
				positionList.get(6).setVisibility(View.VISIBLE);
			}
		} else {
			positionList.get((signDays % 7) - 1).setVisibility(View.VISIBLE);
		}
		// 签到以后位置
		ImageView signed_positon_img1 = (ImageView) this
				.findViewById(R.id.signed_positon_img1);
		ImageView signed_positon_img2 = (ImageView) this
				.findViewById(R.id.signed_positon_img2);
		ImageView signed_positon_img3 = (ImageView) this
				.findViewById(R.id.signed_positon_img3);
		ImageView signed_positon_img4 = (ImageView) this
				.findViewById(R.id.signed_positon_img4);
		ImageView signed_positon_img5 = (ImageView) this
				.findViewById(R.id.signed_positon_img5);
		ImageView signed_positon_img6 = (ImageView) this
				.findViewById(R.id.signed_positon_img6);
		ImageView signed_positon_img7 = (ImageView) this
				.findViewById(R.id.signed_positon_img7);
		List<ImageView> signedPositionList = new ArrayList<ImageView>();
		signedPositionList.add(signed_positon_img1);
		signedPositionList.add(signed_positon_img2);
		signedPositionList.add(signed_positon_img3);
		signedPositionList.add(signed_positon_img4);
		signedPositionList.add(signed_positon_img5);
		signedPositionList.add(signed_positon_img6);
		signedPositionList.add(signed_positon_img7);
		if (signDays % 7 == 0) {
			if (signDays != 0) {
				for (int i = 0; i < 7; i++) {
					signedPositionList.get(i).setVisibility(View.VISIBLE);
				}
			}
		} else {
			for (int i = 0; i < signDays % 7; i++) {
				signedPositionList.get(i).setVisibility(View.VISIBLE);
			}
		}

		// 当前进度
		ProgressBar progressbar = (ProgressBar) this
				.findViewById(R.id.progressbar);
		progressbar.setMax(6);
		if ((signDays % 7) == 0) {
			if (signDays == 0) {
				progressbar.setProgress(0);
			} else {
				progressbar.setProgress(6);
			}
		} else {
			progressbar.setProgress((signDays % 7) - 1);
		}

		ImageView defaultPsition_img1 = (ImageView) this
				.findViewById(R.id.defaultPsition_img1);
		ImageView defaultPsition_img2 = (ImageView) this
				.findViewById(R.id.defaultPsition_img2);
		ImageView defaultPsition_img3 = (ImageView) this
				.findViewById(R.id.defaultPsition_img3);
		ImageView defaultPsition_img4 = (ImageView) this
				.findViewById(R.id.defaultPsition_img4);
		ImageView defaultPsition_img5 = (ImageView) this
				.findViewById(R.id.defaultPsition_img5);
		ImageView defaultPsition_img6 = (ImageView) this
				.findViewById(R.id.defaultPsition_img6);
		ImageView defaultPsition_img7 = (ImageView) this
				.findViewById(R.id.defaultPsition_img7);
		List<ImageView> defaultList = new ArrayList<ImageView>();
		defaultList.add(defaultPsition_img1);
		defaultList.add(defaultPsition_img2);
		defaultList.add(defaultPsition_img3);
		defaultList.add(defaultPsition_img4);
		defaultList.add(defaultPsition_img5);
		defaultList.add(defaultPsition_img6);
		defaultList.add(defaultPsition_img7);
		// 适配
		int screenWidth = SystemUtils.getScreenWidth(this);
		int screenHeight = SystemUtils.getScreenHeight(this);
		LogUtil.e("TAG", "sing" + screenWidth);
		LogUtil.e("TAG", "sing" + screenHeight);
		// 签到以后位置
		// 默认签到位置
		// 当前位置image
		int derta = (screenWidth - SystemUtils.dip2px(this, 60 + 7 * 14)) / 6;
		// 签到以后位置
		// 默认签到位置

		// 当前位置image
		for (int i = 0; i < positionList.size(); i++) {
			android.widget.LinearLayout.LayoutParams params = (LayoutParams) positionList
					.get(i).getLayoutParams();
			if (i == 0) {
				params.leftMargin = SystemUtils.dip2px(this, 5);
			} else {
				params.leftMargin = derta;
			}
			positionList.get(i).setLayoutParams(params);

		}
		// 签到以后位置
		for (int i = 0; i < signedPositionList.size(); i++) {
			android.widget.LinearLayout.LayoutParams params = (LayoutParams) signedPositionList
					.get(i).getLayoutParams();
			if (i == 0) {
				params.leftMargin = SystemUtils.dip2px(this, 5);
			} else {
				params.leftMargin = derta;
			}
			signedPositionList.get(i).setLayoutParams(params);
		}
		// 默认签到位置
		for (int i = 0; i < defaultList.size(); i++) {
			android.widget.LinearLayout.LayoutParams params = (LayoutParams) defaultList
					.get(i).getLayoutParams();
			if (i == 0) {
				params.leftMargin = SystemUtils.dip2px(this, 5);
			} else {
				params.leftMargin = derta;
			}
			defaultList.get(i).setLayoutParams(params);
		}
		RelativeLayout gift_box_rl1 = (RelativeLayout) this
				.findViewById(R.id.gift_box_rl1);
		RelativeLayout gift_box_rl2 = (RelativeLayout) this
				.findViewById(R.id.gift_box_rl2);
		RelativeLayout gift_box_rl3 = (RelativeLayout) this
				.findViewById(R.id.gift_box_rl3);
		RelativeLayout gift_box_rl4 = (RelativeLayout) this
				.findViewById(R.id.gift_box_rl4);
		translateAnimation(gift_box_rl1);
		translateAnimation(gift_box_rl2);
		translateAnimation(gift_box_rl3);
		translateAnimation(gift_box_rl4);
		double signWeek = Math.ceil(signDays / 7.0);
		LogUtil.e("TAG", "signWeek * 7------>" + signWeek * 7);
		// int increasing = 0;
		// if (signWeek * 7 > 28 + increasing) {
		// increasing = (int) (Math.ceil((signWeek * 7 - 28) / 21) * 21);
		// giftday_tv1.setText(((increasing + 28) - 21) + "天");
		// giftday_tv2.setText(((increasing + 28) - 14) + "天");
		// giftday_tv3.setText(((increasing + 28) - 7) + "天");
		// giftday_tv4.setText((increasing + 28) + "天");
		// } else {
		// giftday_tv1.setText("7天");
		// giftday_tv2.setText("14天");
		// giftday_tv3.setText("21天");
		// giftday_tv4.setText("28天");
		// }

	}

	/********
	 * 补签弹窗
	 ********/
	public void retroactiveDialog(int coinCount, List<String> dataList,
			String reSignExt) {
		if (retroactiveDialog != null) {
			retroactiveDialog.cancel();
			retroactiveDialog = null;
		}
		retroactiveDialog = DialogUtils.retroactiveDialog(this, coinCount,
				dataList, reSignExt, new OnClickListener() {

					@Override
					public void onClick(View v) {
						switch (v.getId()) {
						case R.id.retroactive_bt: // 补签 Button
							repairSignIn();
							if (retroactiveDialog != null) {
								retroactiveDialog.cancel();
								retroactiveDialog = null;
							}
							break;
						case R.id.return_bt: // 还是算了
							if (retroactiveDialog != null) {
								retroactiveDialog.cancel();
								retroactiveDialog = null;
							}
							break;
						case R.id.close_img: // Xbutton
							if (retroactiveDialog != null) {
								retroactiveDialog.cancel();
								retroactiveDialog = null;
							}
							break;
						}
					}
				});
		retroactiveDialog.show();
	}

	/*******
	 * 签到页接口
	 *******/
	public void signInPageInterface() {
		KkdHttpMagager.getInstance().getSignInPageInterface(SingInData.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (SingInData) responseBody;
						mHandler.sendEmptyMessage(SIGN_CODE_ZREO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						SignActivity.this.desc = desc;
						mHandler.sendEmptyMessage(SIGN_CODE_ELSE);
					}
				});
	}

	/*********
	 * 签到提醒开关接口
	 *********/
	public void updateRemindSatus(int status) {
		// status 签到提醒开关状态码 0-关闭，1-开启
		KkdHttpMagager.getInstance().updateRemindSatus(status, String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						mHandler.sendEmptyMessage(SIGN_SWITCH_ZREO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						SignActivity.this.desc = desc;
						mHandler.sendEmptyMessage(SIGN_SWITCH_ELSE);
					}
				});
	}

	/*****************
	 * 签到页 补签数据接口
	 *****************/
	public void beforeRepairSignIn() {
		KkdHttpMagager.getInstance().beforeRepairSignIn(SingInData.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						SingInData data = (SingInData) responseBody;
						Message msg = new Message();
						msg.obj = data;
						msg.what = RETROACTIVE_DATA_OK;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = desc;
						msg.what = RETROACTIVE_DATA_NO;
						mHandler.sendMessage(msg);
					}
				});
	}

	// 补签接口
	public void repairSignIn() {
		KkdHttpMagager.getInstance().repairSignIn(String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = new Message();
						msg.what = REPAIRSIGNIN_OK;
						msg.obj = responseBody;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = REPAIRSIGNIN_NO;
						msg.obj = desc;
						mHandler.sendMessage(msg);

					}
				});
	}

	// 进度条小礼盒动画
	public void scalAnimation(View view) {
		ScaleAnimation scale = new ScaleAnimation(0f, 1f, 0f, 1f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				1.0f);
		scale.setDuration(1000);
		view.startAnimation(scale);
	}

	// 带线的大礼品盒动画
	public void translateAnimation(View view) {
		PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(
				"translationY", SystemUtils.dip2px(this, -34), 1f);
		animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhX);
		animator.setInterpolator(new OvershootInterpolator());
		animator.setDuration(1000);
		animator.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (retroactiveDialog != null) {
			retroactiveDialog.cancel();
			retroactiveDialog = null;
		}
		if (signdays1RotationX != null) {
			signdays1RotationX.end();
			signdays1RotationX.cancel();
			signdays1RotationX = null;
		}
		if (animator != null) {
			animator.cancel();
			animator = null;
		}
	}
}
